#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;

mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
ll n, k;

ll rev(ll n, ll p) {
  ll res = 0;
  while (n) {
    res = res * p + n % p;
    n /= p;
  }
  return res;
}
ll sum2(ll n) { return n * (n + 1) / 2 * (n * 2 + 1) / 3 % MOD; }
ll f(ll L, ll R) {
  ll res = R + 1 - L;
  res = (res - sum2(R) + sum2(L - 1) + MOD) % MOD;
  return res;
}
ll solve() {
  cin >> n >> k;
  ll sum = 0;
  ll x = sqrt(n);
  rep(i, 2, min(x, k)) sum = (sum + rev(n, i)) % MOD;
  if (x >= k) return sum;
  if (k > n) sum = (sum + (k - n) % MOD * n % MOD) % MOD;
  ll mn = x + 1, mx = min(k, n);
  ll pn = (mn + mx) * (mx + 1 - mn) / 2 % MOD * n % MOD;
  sum = (sum + pn) % MOD;
  for (ll L = mn, R; L <= mx; L = R + 1) {
    R = min(mx, n / (n / L));
    ll now = (n / L) * f(L, R) % MOD;
    sum = (sum + now) % MOD;
  }
  return sum;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) cout << solve() << endl;
  return 0;
}